package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import orm.CollectivityHomework;
import orm.Teacher;
import util.DBHelper;

public class CollectivityHomeworkDAO {
	
	
	public void insertchw(Statement statement, CollectivityHomework chw, String tpid) throws SQLException {
		statement.execute("insert into collectivityhomework values('" + tpid + "','" + chw.getClassName() + "','" +
				chw.getHomeworkDate() + "', '" + chw.getTeacherID() + "', '" + chw.getTeacherName() + "')");
	}
	
	public void updatechw(Statement statement, CollectivityHomework chw, String tpid) throws SQLException {
		statement.execute("update collectivityhomework set ClassName = '" +
				chw.getClassName() + "', HomeworkDate = '" + chw.getHomeworkDate() + 
				"', TeacherID = '" + chw.getTeacherID() + "', TeacherName = '" + 
				chw.getTeacherName() + "' where TPID = '" + tpid + "'");
	}
	
	public void deletechw(Statement statement, String tpID) throws SQLException {
		statement.execute("delete from collectivityhomework where TPID='" + tpID + "'");
	}
	
	
	public CollectivityHomework getCHWByTPID(String tpID) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		CollectivityHomework chw = new CollectivityHomework();
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery("select * from collectivityhomework where TPID='" + tpID + "'");
		
		while (rs.next()) {
			int i = 1;
			chw.setTPID(rs.getString(i++));
			chw.setClassName(rs.getString(i++));
			chw.setHomeworkDate(rs.getString(i++));
			chw.setTeacherID(rs.getInt(i++));
			chw.setTeacherName(rs.getString(i++));
			
		}
		
		rs.close();
		stmt.close();
		conn.close();
		
		return chw;
	}
	
}
